package com.etc.controller;

import com.etc.entity.CommentResult;
import com.etc.entity.Infobar;
import com.etc.service.InfobarService;
import com.etc.service.ParkService;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
public class InfobarController {
    @Autowired
    private InfobarService infobarService;

    @Autowired
    private ParkService parkService;

    @RequestMapping("infobar/findById/{id}")
    public CommentResult findById(@PathVariable("id")Integer id)
    {
        Infobar result = infobarService.findById(id);
        List<Map<String,Object>> result1 = new ArrayList<>();

            Map<String,Object> map = new HashMap<>();
            map.put("id",result.getId());
            map.put("info",result.getInfo());
            map.put("img",result.getImg());
            map.put("upload_date",result.getUpload_date());
            map.put("park_id",result.getPark_id());
            map.put("park_name",parkService.findById(result.getPark_id()).getPark_name());
            result1.add(map);


        return new CommentResult(200,"success",result1);
    }

    @RequestMapping("infobar/findByPark/{id}")
    public CommentResult findByPark(@PathVariable("id")Integer id,@RequestParam(value = "start", required = false) Integer start,
                                    @RequestParam(value = "size", required = false) Integer size)
    {

        List<Infobar> list = infobarService.findByPark(id);
        List<Map<String,Object>> result = new ArrayList<>();
        for (int i = 0; i < list.size(); i++) {
            Map<String,Object> map = new HashMap<>();
            map.put("id",list.get(i).getId());
            map.put("info",list.get(i).getInfo());
            map.put("img",list.get(i).getImg());
            map.put("upload_date",list.get(i).getUpload_date());
            map.put("park_id",list.get(i).getPark_id());
            map.put("park_name",parkService.findById(list.get(i).getPark_id()).getPark_name());
            result.add(map);
        }

        return new CommentResult(200,"success",result);
    }

    @RequestMapping("infobar/findAllInfobar")
    public CommentResult findAllInfobar(@RequestParam(value = "start", required = false) Integer start,
                                    @RequestParam(value = "size", required = false) Integer size)
    {

        PageHelper.startPage(start,size);
        List<Infobar> list = infobarService.findAllInfobar();
        List<Map<String,Object>> result = new ArrayList<>();
        for (int i = 0; i < list.size(); i++) {
            Map<String,Object> map = new HashMap<>();
            map.put("id",list.get(i).getId());
            map.put("info",list.get(i).getInfo());
            map.put("img",list.get(i).getImg());
            map.put("upload_date",list.get(i).getUpload_date());
            map.put("park_id",list.get(i).getPark_id());
            map.put("park_name",parkService.findById(list.get(i).getPark_id()).getPark_name());
            result.add(map);
        }

        return new CommentResult(200,"success",result);
    }


}
